package cn.kgc.ubagnet.service.buyer;

import cn.kgc.ubagnet.mapper.BuyerMapper;
import cn.kgc.ubagnet.pojo.Buyer;
import cn.kgc.ubagnet.pojo.BuyerExample;
import cn.kgc.ubagnet.pojo.SecurityBuyer;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
public class CrowdBuyerDetailService implements UserDetailsService {
    public static String phone;
    @Resource
    BuyerMapper buyerMapper;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        BuyerExample example=new BuyerExample();
        BuyerExample.Criteria criteria = example.createCriteria();
        criteria.andPhoneEqualTo(username);
        List<Buyer> buyers = buyerMapper.selectByExample(example);
        if(buyers.size()>0&&buyers!=null){
            Buyer buyer=buyers.get(0);
            this.phone=buyer.getPhone();
            List<GrantedAuthority> authorities=new ArrayList<>();
            authorities.add(new SimpleGrantedAuthority("vip1"));
            SecurityBuyer securityBuyer = new SecurityBuyer(buyer, authorities);
        return securityBuyer;
        }
        return null;
    }
}
